package com.ncui.wisdomLearn.biz.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ncui.wisdomLearn.api.entity.LoginLog;
import com.ncui.wisdomLearn.biz.user.service.LoginLogService;
import com.ncui.wisdomLearn.common.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 登录日志 控制层
 *
 * @author lvlele
 * @since 2025-01-07 15:12:13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/loginLog")
public class LoginLogController {

    private final LoginLogService loginLogService;

    /**
     * 分页查询用户登录日志
     *
     * @param page 分页对象
     * @param userId 用户ID
     * @return 分页数据
     */
    @GetMapping("/page")
    public R<Page<LoginLog>> page(Page<LoginLog> page, @RequestParam(required = false) Long userId) {
        LambdaQueryWrapper<LoginLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId != null, LoginLog::getUserId, userId)
                .orderByDesc(LoginLog::getLoginTime);
        return R.ok(loginLogService.page(page, wrapper));
    }

    /**
     * 获取用户最后一次登录记录
     *
     * @param userId 用户ID
     * @return 登录记录
     */
    @GetMapping("/last/{userId}")
    public R<LoginLog> getLastLoginRecord(@PathVariable Long userId) {
        return R.ok(loginLogService.getLastLoginRecord(userId));
    }
}

